<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加动画</el-button>
    </div>
    <el-table :data="animationList" border style="width: 100%;" >
      <el-table-column prop="id" label="ID" width="100px">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="150px">
      </el-table-column>
      <el-table-column label="标题图片" width="200px">
        <template slot-scope="scope">
          <img :src="baseUrl+scope.row.img" width="100%"/>
          <el-button type="text" @click="photobox(1,scope.row)">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column label="内容" width="200px">
        <template slot-scope="scope">
          <embed :src="baseUrl+scope.row.content" autoplay muted loop width="100%"  />
          <el-button type="text" @click="swfbox(2,scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="请在下方选择您要上传的图片" :visible.sync="tupian" width="60%">
      <uploadphoto :file="file" v-on:func="func"></uploadphoto>
    </el-dialog>
    <el-dialog title="请在下方选择您要上传的图片" :visible.sync="swf" width="60%">
      <uploadanimation :file="file" v-on:func="func"></uploadanimation>
    </el-dialog>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="标题" prop="title">
          <el-input v-model="temp.titile"></el-input>
        </el-form-item>
        <el-form-item label="" prop="img">
        <uploadphoto :file="file" v-on:func="func"></uploadphoto>
        </el-form-item>
        <el-form-item label="" prop="animation">
        <uploadanimation :file="file" v-on:func="func"></uploadanimation>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { fetchAnimationList, addAnimation } from '@/api/infomation'
  import uploadphoto from '../utils/uploadphoto'
  import uploadanimation from '../utils/uploadanimation'
  export default {
    components: {
      uploadphoto,
      uploadanimation
    },
    data() {
      return {
        animationList: null,
        total: null,
        tupian: false,
        swf: false,
        uploadForm: new FormData(),
        file: {
          indexRow: '',
          methodurl: 'updateAnimationPhotoById.do',
          index: ''
        },
        listQuery: {
          page: 1,
          size: 10
        },
        textMap: {
          update: 'Edit',
          create: 'Create'
        },
        temp: {
          title: '',
          img: '',
          animation: ''
        },
        dialogPvVisible: false,
        dialogFormVisible: false,
        dialogStatus: ''
      }
    },
    created() {
      this.getAnimationList()
    },
    methods: {
      getAnimationList() {
        fetchAnimationList(this.listQuery).then(response => {
          this.animationList = response.data.rows
          this.total = response.data.total
        })
      },
      swfbox(index, row) {
        this.swf = true
        this.file.indexRow = row
        this.file.index = index
      },
      photobox(index, row) {
        this.tupian = true
        this.file.indexRow = row
        this.file.index = index
      },
      func() {
        this.tupian = false
        this.swf = false
        this.getAnimationList()
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getUserList()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getUserList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getUserList()
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.uploadForm.append('title', this.temp.title)
            this.uploadForm.append('img', this.temp.img)
            this.uploadForm.append('content', this.temp.animation)
            addAnimation(this.uploadForm).then(res => {
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
  .app-container{
    padding: 30px;
  }
</style>
